import random
import networkx as nx
import matplotlib.pyplot as plt

#随机生成集合
def createset(m,n):
    A=random.sample(range(m),n)#在A中任取n个元素，range(m)表示生成0~m-1的自然数集合
    A=set(A)#set()用于创建一个集合，允许存储不重复元素集合
    return A

#实现笛卡尔乘积，AxB
def Cartesianproduct(X,Y):
    XY=set({})
    for x in X:
        for y in Y:
            if(x!=y):
                XY.add((x,y))
    return XY

#求幂集
def subset(V,n,m):
    element=()
    k=0
    while(m!=0):
        if(n%2==1):
            element=element+tuple([V[k]])
        k=k+1
        n=(n-n%2)/2
        m=m-1
    return element

def powerset(V):
    m=len(V)
    n=2**m
    power=set({})
    for k in range(n):
        element=subset(V,k,m)
        power=power|{element}
    return power

#将集合关系图直观地显示出来
def drawgraph(R):
    G=nx.DiGraph()
    G.add_edges_from(R)
    nx.draw(G,node_size= 200,node_color='r', with_labels= True, font_color='w')
    plt.show()
    return


